# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.

project("jnidemo")

#设置指定的C++编译器版本是必须的，如果不设置，或者为OFF，则指定版本不可用时，会使用上一版本。
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#指定为C++11 版本
set(CMAKE_CXX_STANDARD 11)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

#file包含相应目录下的cpp/c文件 并用SOURCE指代
#TO_CMAKE_PATH会将路径转换成cmake风格的路径表达形式
file(GLOB SOURCE util/*.cpp util/*.c *.cpp *.c)

file(GLOB FILES util/*.cpp util/*.c  util/*.h  *.cpp *.c)


#EXCLUDE_FROM_ALL --- 要构建的target会被排除在all target列表之外,当执行make或make all时,该target不会被编译.
#add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [<source>...])

add_library( # Sets the name of the library.
        jnidemo

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        ${SOURCE})

add_library( # Sets the name of the library.
        otherlib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        util/jni_helper.cpp
        other_lib.cpp)

add_library( # Sets the name of the library.
        cpluspluslearnlib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        cplusplus-learn-lib.cpp)


add_library( # Sets the name of the library.
        fmoduselib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        fmod-use-lib.cpp)

add_library( # Sets the name of the library.
        jniinitlib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        jni_init_lib.cpp)

#添加第三方so库方式---start
#方式一==============================================================================================
#[[add_library(
        ndkjnidemo
        SHARED
        IMPORTED)

set_target_properties(
        ndkjnidemo
        PROPERTIES IMPORTED_LOCATION
        ./---当前目录
        ../---上一级目录
        ${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libndkjnidemo.so
)

target_include_directories(
        jnidemo
        PRIVATE
        include
)]]

#方式二==============================================================================================
#包含头文件目录
include_directories("include")
include_directories("includefmod")

add_library(
        ndkjnidemo
        SHARED
        IMPORTED)

set_target_properties(
        ndkjnidemo
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libndkjnidemo.so
)

add_library(
        libfmod
        SHARED
        IMPORTED)

set_target_properties(
        libfmod
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libfmod.so
)

add_library(
        libfmodL
        SHARED
        IMPORTED)

set_target_properties(
        libfmodL
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libfmodL.so
)

#方式三==============================================================================================
#测试未成功
#[[include_directories("include")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}")]]

#添加第三方so库方式---end

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        jnidemo
        ndkjnidemo
        #libfmod
        #libfmodL
        fmoduselib
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

#单独链接  解决cpp文件找不到log日志问题
target_link_libraries( # Specifies the target library.
        otherlib
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

#单独链接  解决cpp文件找不到log日志问题
target_link_libraries( # Specifies the target library.
        cpluspluslearnlib
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

target_link_libraries( # Specifies the target library.
        jniinitlib
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

#该命令用于增加一个构建的子目录  CMake命令会执行完子目录中的CMakeLists.txt文件之后,再执行后面的命令.
#add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])


#CMake 常用变量
    #名称	                                                 含义
#CMAKE_SOURCE_DIR	                   源码的根目录，也就是最 top 层 CMakeLists.txt 文件存在的目录
#CMAKE_BINARY_DIR	                   编译的binary的根目录，也就是运行CMake时的目录，一般就是 build 目录
#CMAKE_CURRENT_SOURCE_DIR	           当前正在运行的 CMakeLists.txt 文件的目录
#CMAKE_CURRENT_BINARY_DIR	           当前正处于的编译目录，例如如果正在运行src目录下的CMakeLists.txt， 该变量通常为 build/src 目录
#CMAKE_CXX_FLAGS 和 CMAKE_C_FLAGS	   当使用 g++ (或 gcc)编译文件时，用于设置 compiler flags, 也就是宏定义
#CMAKE_CXX_STANDARD	                   该变量用于编译target时，使用的C++标准，支持 98, 11, 14, 17, 20, 23. 该变量的作用原理是：用于初始化 target 的CXX_STANDARD 属性。
#CMAKE_BUILD_TYPE	                   编译类型。一般发布时使用 release，调试时使用 debug
